/***************************************************************************
 *   Copyright (C) 2007 by Marcin Swiechowicz   *
 *   marcin.swiechowicz@gmail.com   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include "mainwindow.h"

#include "kdcfilefactory.h"
#include "kdcconfig.h"

#include <kdclib.h>

#include <stdio.h>
#include <stdlib.h>

#include <QDir>
#include <QString>
#include <QFileInfo>
#include <QList>
#include <QRegExp>
#include <QPixmap>
#include <QBitmap>
#include <QObject>

#include <KApplication>
#include <KAboutData>
#include <KCmdLineArgs>
#include <kio/netaccess.h>
#include <kio/job.h>
#include <KStandardDirs>
#include <KUrl>
#include <KBugReport>
#include <KMessageBox>

void myMessageOutput( QtMsgType type, const char *msg )
{
	KDCFileFactory::getInstance()->writeLogMessage( msg );
	switch ( type )
	{
		case QtDebugMsg:
		{
			fprintf( stderr, "Debug: %s\n", msg );
			break;
		}
		case QtWarningMsg:
		{
			fprintf( stderr, "Warning: %s\n", msg );
			break;
		}
		case QtCriticalMsg:
		{
			fprintf( stderr, "Critical: %s\n", msg );
			break;
		}
		case QtFatalMsg:
		{
			fprintf( stderr, "Fatal: %s\n", msg );
			abort();
		}
	}
}

bool checkStartupFiles()
{
	if ( !KIO::NetAccess::exists( KUrl( QDir::homePath() + "/.spumux/kdcfont.ttf" ), TRUE, NULL ) )
	{
		QString src = KStandardDirs::locate("appdata", "data/kdcfont.ttf" );
		if ( createDirectory( QDir::homePath() + "/.spumux" ) )
			return KIO::NetAccess::copy( KUrl( src ), KUrl( QDir::homePath() + "/.spumux/kdcfont.ttf" ), NULL );
		else
			return FALSE;
	}
	else
		return TRUE;
}

void checkPreviousSession()
{
	KBugReport bugReport;
	QRegExp reg( "^\\KDVDCreator(\\c*)" );

	QDir path( QDir::homePath() + "/.kde4/tmp-linux/" );
	QFileInfoList dirList = path.entryInfoList( QDir::Dirs );
	QList<QFileInfo>::const_iterator it;
	QString name;
	QString fullPath;
	//KDCDeleteJob *job;
	for ( it = dirList.constBegin(); it != dirList.constEnd(); ++it )
	{
		name = ( *it ).baseName();
		fullPath = ( *it ).path() + "/" + name;
		if ( name.contains( reg ) )
		{
			if ( QFile::exists( fullPath + "/kdclog.log" ) && KDCConfig::bugReport() )
			{
				//bugReport.setMessageBody( KDCFileFactory::readAll( fullPath + "/kdclog.log" ) );
				//bugReport.exec();
			}
			//job = new KDCDeleteJob;
			//job->addFile( fullPath );
			//job->start();
		}
	}
	dirList = path.entryInfoList( QDir::Files );
	for ( it = dirList.constBegin(); it != dirList.constEnd(); ++it )
	{
		name = ( *it ).baseName();
		if ( name.contains( reg ) )
		{
			//job = new KDCDeleteJob;
			//job->addFile( ( *it ).absoluteFilePath() );
			//job->start();
		}
	}
}

int main( int argc, char *argv[] )
{
	KAboutData aboutData(	"KDVDCreator", "KDVDCreator", ki18n("KDVDCreator"), "0.4",
							ki18n("Creating VCD, SVCD and DVD discs."), KAboutData::License_GPL,
							ki18n("(c) 2007"), ki18n("I need help with icons, splash, tray icon."),
							"http://sourceforge.net/projects/kdvdcreator", "marcin.swiechowicz@gmail.com" );
	KCmdLineArgs::init( argc, argv, &aboutData );

	KApplication app;
	qInstallMsgHandler( myMessageOutput );
/*
	QString src = KStandardDirs::locate( "appdata", "data/splash.png" );
	qDebug( src.toUtf8() );
	if ( !checkStartupFiles() )
	{
		KMessageBox::error( 0, QString( "Missing program files" ) );
		return -1;
	}
	checkPreviousSession();
	if ( !KDCFileFactory::getInstance() )
		return -1;
*/
	//try
	//{
		MainWindow *window = new MainWindow();
		window->show();
		return app.exec();
	//}
	//catch (...)
	//{
	//	fprintf(stderr, "Aborted by error" );
	//}
	//return 0;
}
